package com.nantai.jiafang.nantaijiafang.activity.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.SewingDetail;
import com.nantai.jiafang.nantaijiafang.databinding.ActivitySewingDetailBinding;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhijianDetailItemBinding;

import java.util.List;

public class SewingDataAdapter extends BaseAdapter implements View.OnClickListener {

    private Context mContext;

    private List<SewingDetail> list;

    private Callback mCallBack;

    public SewingDataAdapter(Context context,List<SewingDetail> list,Callback mCallBack){
        this.mContext = context;

        this.list = list;

        this.mCallBack = mCallBack;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    public void setList(List<SewingDetail> list){
        this.list.addAll(list);
    }

    public void refreshList(List<SewingDetail> list){
        this.list = list;
    }


    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        SewingDetail detail = list.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_sewing_detail, null);
            viewHolder = new ViewHolder();
            viewHolder.cInvName = (TextView) convertView.findViewById(R.id.cInvName);
//            viewHolder.colourName = convertView.findViewById(R.id.colourName);
            viewHolder.packQty = (TextView) convertView.findViewById(R.id.packQty);
            viewHolder.inspectorName = (TextView) convertView.findViewById(R.id.inspectorName);
//            viewHolder.comNum = (TextView) convertView.findViewById(R.id.comNum);
//            viewHolder.checkRate_HF = (TextView) convertView.findViewById(R.id.checkRate_HF);
            viewHolder.facArea = convertView.findViewById(R.id.facArea);
            viewHolder.weekNo = (TextView) convertView.findViewById(R.id.weekNo);
            viewHolder.defectNum = (TextView) convertView.findViewById(R.id.defectNum);
            viewHolder.defectDes = (TextView) convertView.findViewById(R.id.defectDes);
            viewHolder.groupName = (TextView) convertView.findViewById(R.id.groupName);
            viewHolder.inspectTime = (TextView) convertView.findViewById(R.id.inspectTime);
            viewHolder.colourName = (TextView) convertView.findViewById(R.id.colourName);
            viewHolder.btnDel = (Button) convertView.findViewById(R.id.btn_del);//衍缝巡检删除
            viewHolder.btnDel.setOnClickListener(this);
            viewHolder.btnDel.setTag(position+"");
            viewHolder.btnEdit = (Button) convertView.findViewById(R.id.btn_edit);//衍缝巡检修改
            viewHolder.btnEdit.setOnClickListener(this);
            viewHolder.btnEdit.setTag(position+"");
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.cInvName.setText(detail.getcInvName());
        viewHolder.packQty.setText(detail.getPackQty());
        viewHolder.facArea.setText(detail.getFacArea());
        viewHolder.inspectorName.setText(detail.getInspectorName());
//        viewHolder.colourName.setText(detail.getColourName());
//        viewHolder.comNum.setText(detail.getComNum());
//        viewHolder.checkRate_HF.setText(detail.getCheckRate_HF());
        viewHolder.weekNo.setText(detail.getWeekNo());
        viewHolder.defectNum.setText(detail.getDefectNum());
        viewHolder.defectDes.setText(detail.getDefectDes());
        viewHolder.groupName.setText(detail.getGroupName());
        viewHolder.inspectTime.setText(detail.getInspectTime());
        viewHolder.colourName.setText(detail.getColourName());
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_del:
                String position = (String) v.getTag();
                SewingDetail detail = list.get(Integer.parseInt(position));
                list.remove(Integer.parseInt(position));
                mCallBack.delete(detail.getGuid());
                break;
            case R.id.btn_edit:
                String position1 = (String) v.getTag();
                SewingDetail detail1 = list.get(Integer.parseInt(position1));
                mCallBack.detail(detail1);
                break;
        }
    }

    public interface Callback{
         void delete(String guid);

         void detail(SewingDetail detail);
    }


    static class ViewHolder{

        TextView cInvName,packQty,inspectorName,comNum,facArea,weekNo,defectNum;
        TextView defectDes,groupName,inspectTime,colourName;
        Button btnEdit,btnDel;
    }
}
